package com.war.controller.safeManage;

import com.war.pojo.system.User;
import com.war.service.safeManage.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@RequestMapping("/login") //浏览器访问路径一级
public class LoginController {
    @Autowired
    UserService userService;

    //访问登录页面login.html，使用login方法服务
    @RequestMapping("/loginPage") //浏览器访问路径二级
    public String loginPage() {
        //返回html文件路径
        return "safeManage/login";
    }

    //手机号登录
    @RequestMapping("/telLogin")
    @ResponseBody
    public HashMap<String,Object> telLogin(User user, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.login(user, request);
        map.put("info",info);
        return map;
    }

    //验证码邮件发送
    @RequestMapping("/sendCode")
    @ResponseBody
    public HashMap<String, Object> sendCode(String email, HttpServletRequest request){
        return userService.sendCode(email, request);
    }

    //邮箱登录
    @RequestMapping("/emailLogin")
    @ResponseBody
    public HashMap<String, Object> emailLogin(String code, HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        //取出存在session中的验证码的值
        String valCode = session.getAttribute("valCode")+"";
        //判断用户输入的验证码和邮箱接收的验证码是否一致
        if(code.equals(valCode)){
            map.put("info", "邮箱登录成功");
        }else {
            map.put("info", "验证码输入错误");
        }
        return map;
    }
}
